<?php 
function GetCategories()
{
        $conn = getConnection();
        $result = $conn->query('SELECT * FROM Categories Cat');
        return $result;
}

function GetCategory($id)
{
        $conn = getConnection();
        $result = $conn->query("SELECT * FROM Categories Cat WHERE ID=$id");
        $rs = $result->fetch_assoc();
        $conn->close();
        return $rs;
}

function SaveCategory($rs)
{ 
		$conn = getConnection();
		$rs = RsEscape($conn, $rs);
        $result = $conn->query("UPDATE Category Cat SET Name='$rs[Name]' WHERE ID=$rs[ID]");     
        $error = $conn->error;
        $conn->close();
        return $error ? array('Server Error'=> $error) : Null;
        
}

function ValidateCategory($rs)
{ 
	$errors = Null;
	if(empty($rs['Name']))
	{
		$errors['Name'] = 'A category must have a name';
	}
	return $errors;
	
}

function DeleteCategory($id)
{
	
		$conn = getConnection();
        $result = $conn->query("DELETE FROM Categories WHERE ID=$id");     
        $error = $conn->error;
        $conn->close();
		return $error ? array('Server Error'=> $error) : Null;
	
}

function AddCategory($rs)
{
		$conn = getConnection();
		$rs = RsEscape($conn, $rs);
		$sql = "INSERT INTO Categories" 
			   ."(Name, Created_at)"
			   ."VALUES ('$rs[Name]', Now())";
        $result = $conn->query($sql);     
        $error = $conn->error;
        $conn->close();
		return $error ? array('Server Error'=> $error) : Null;
	
}

function NewCategory()
{
	return array('Name'=>'', 'ID'=>Null, 'Created_at'=>Null, 'Updated_at'=>Null);
}

?>